package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.EntryInformationAddDTO;
import com.oa.pm.dto.EntryInformationAuditDTO;
import com.oa.pm.dto.EntryInformationListDTO;
import com.oa.pm.dto.EntryInformationUpdateDTO;
import com.oa.pm.entity.EntryInformation;
import com.oa.pm.service.IEntryInformationService;
import com.oa.pm.vo.EntryInformationListVO;
import com.oa.pm.vo.EntryInformationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/entryInformation")
@Api(description = "入职信息表(个人资料表)模块")
public class EntryInformationController extends BaseController {

    private final IEntryInformationService entryInformationService;

    @Autowired
    public EntryInformationController(IEntryInformationService entryInformationService) {
        this.entryInformationService = entryInformationService;
    }

    @GetMapping("")
    @CommonController
    @Description("关联查询")
    @ApiOperation(value = "关联查询")
    public Resp<EntryInformationVO> findEntryInformation(@ApiParam("用户Id") @RequestParam(value = "id", required = false) String id) {
        if (id == null) {
            return new Resp().success(entryInformationService.findEntryInformation("", getLoginUserId()));
        }
        return new Resp().success(entryInformationService.findEntryInformation(id, ""));
    }


    /**
     * 保存
     *
     * @param
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "入职信息表(个人资料表)对象") @RequestBody(required = false) EntryInformationAddDTO entryInformationAddDTO) throws BusinessException, DataIsEmptyException {
        entryInformationAddDTO.setUserId(getLoginUserId());
        return entryInformationService.addEntryInformation(entryInformationAddDTO);
    }

    /**
     * 保存
     *
     * @param
     * @return Resp
     */
    @PutMapping("/put")
    @CommonController
    @Description("修改")
    @ApiOperation(value = "修改")
    public Resp put(@ApiParam(value = "入职信息表(个人资料表)对象") @RequestBody(required = false) EntryInformationUpdateDTO entryInformationUpdateDTO) {
        EntryInformation entryInformation = new EntryInformation();
        BeanUtils.copyProperties(entryInformationUpdateDTO, entryInformation);
        if (entryInformationService.updateById(entryInformation)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    @GetMapping("/list")
    @CommonController
    @Description("查询入职列表")
    @ApiOperation(value = "关联查询")
    public Resp<EntryInformationListVO> findEntryInformationList(EntryInformationListDTO entryInformationListDTO) {
        entryInformationListDTO.setUserId(getLoginUserId());
        return new Resp().success(entryInformationService.findEntryInformationList(entryInformationListDTO));
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审核")
    @ApiOperation(value = "审核")
    public Resp audit(@ApiParam("审核对象") @RequestBody EntryInformationAuditDTO entryInformationAuditDTO) throws DataIsEmptyException, BusinessException {
        return entryInformationService.audit(entryInformationAuditDTO);
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(EntryInformationListDTO entryInformationListDTO) {
        entryInformationListDTO.setUserId(getLoginUserId());
        entryInformationService.export(entryInformationListDTO, response);
    }
}
